// ==UserScript==
// @namespace siur2@yahoo.com
// @name Pardus combat share
// @include http://*.pardus.at/combat_details.php*
// ==/UserScript==

var servers = [
	{
		name: "Fed R&D test site",
		url: "http://fed_orion.byethost6.com/logger",
		accounts: {orion: {name: "FalCLogG3R", password: "1099pWd"}}
	}
];

function saveCombat() {
	var select = document.getElementById("sltServer");
	var server = servers[select.selectedIndex];

	function extractStr(source, begin, end) {
		var re = new RegExp(begin, "i");
		var i = source.search(re) + begin.length;
		var str = source.substring(i);
		re = new RegExp(end, "i");
		var j = str.search(re);
		return str.substring(0, j);
	}

	function trim(str) {
		var result = str.replace(/^\s+/, "");
		return result.replace(/\s+$/, "");
	}

	function prepareName(name) {
		var i = name.indexOf("(");
		return trim(i == -1 ? name : name.substr(0, i));
	}

	function append_input(form, name, value) {
		var input = document.createElement("input");
		input.type = "hidden";
		input.name = name;
		input.value = value;
		form.appendChild(input);
	}

	var btnShare = document.getElementById("btnShare"); 
	btnShare.disabled = "true";
	btnShare.className = "disabled";

	var cmbt = new Object();

	var str = document.location.search;
	cmbt.pid = str.substring(str.indexOf("=") + 1);
	str = document.location.hostname;
	var universe = str.substring(0, str.indexOf("."));
	var account = server.accounts[universe];
	if (!account)
		return;
	cmbt.universe = universe.charAt(0).toUpperCase() + universe.substring(1);

	var html = document.body.innerHTML;
	var elem = document.getElementById("report").previousSibling;
	while (elem && !elem.tagName)
		elem = elem.previousSibling;
	str = elem.innerHTML;
	var values = str.split(/<br>/i);
	cmbt.type = values[0];
	cmbt.additional = values[1];

	//Get combat time in GMT
	var missionTime = new Date(values[values.length - 2].replace(/-/g, "/"));
	//alert (missionTime);
	var mtMill = missionTime.getTime();
	//alert(mtMill);
	var offset = missionTime.getTimezoneOffset() * 60000;
	//alert(offset);
	var ct = mtMill + offset;
	alert(mtMill + '\n' + offset+ '\n' + ct);
	cmbt.when = ct;

	str = values[values.length - 1];
	var delim = null;
	if (str.search(/font/i) != -1) {
		delim = /<font.+font>/i;
		cmbt.outcome = extractStr(str, ">", "<");
	} else {
		var delims = ["disengaged from battle with", "was defeated by", "defeated"];
		var outcomes = ["disengaged", "was defeated by", "defeated"];
		for (var i = 0; i < delims.length; i++)
			if (str.indexOf(delims[i]) != -1) {
				delim = delims[i];
				cmbt.outcome = outcomes[i];
				break;
			}
	}
	values = str.split(delim);
	cmbt.attacker = prepareName(values[0]);
	cmbt.defender = prepareName(values[1]);
	str = extractStr(html, "confrontation in ", "]");
	values = str.split(" [");
	cmbt.sector = values[0];
	cmbt.coords = values[1];
	cmbt.data = extractStr(html, "cr = \"", "\"");

	var form = document.getElementById("combatPost");
	if (form)
		document.body.removeChild(form);
	form = document.createElement("form");
	form.style.display = "none";
	form.action = server.url + "/combat_add.php";
	form.method = "post";
	form.target = "combatFrame";
	form.id = "combatPost";
	append_input(form, "acc", account.name);
	append_input(form, "pwd", account.password);
	append_input(form, "pid", cmbt.pid);
	append_input(form, "type", cmbt.type);
	append_input(form, "when", cmbt.when);
	append_input(form, "universe", cmbt.universe);
	append_input(form, "sector", cmbt.sector);
	append_input(form, "coords", cmbt.coords);
	append_input(form, "attacker", cmbt.attacker);
	append_input(form, "defender", cmbt.defender);
	append_input(form, "outcome", cmbt.outcome);
	append_input(form, "additional", cmbt.additional);
	append_input(form, "data", cmbt.data);
	document.body.appendChild(form);
	var frame = document.getElementById("combatFrame");
	if (!frame) {
		frame = document.createElement("iframe");
		frame.name = "combatFrame";
		frame.id = frame.name;
		frame.style.display = "none";
		document.body.appendChild(frame);
	}
	form.submit();
}

function sltServer_onChange() {
	var btnShare = document.getElementById("btnShare"); 
	btnShare.disabled = false;
	btnShare.className = "";
}

function addShareBtn() {
	var div = document.createElement("div");
	var label = document.createElement("b");
	label.appendChild(document.createTextNode("Share combat log at "));
	div.appendChild(label);
	var select = document.createElement("select");
	select.id = "sltServer";
	for (var i = 0; i < servers.length; i++) {
		var option = document.createElement("option");
		option.text = servers[i].name;
		option.value = i;
		select.appendChild(option);
	}
	select.addEventListener("change", sltServer_onChange, false);
	div.appendChild(select);
	div.appendChild(document.createTextNode(" "));
	var btnShare = document.createElement("input");
	btnShare.id = "btnShare";
	btnShare.type = "button";
	btnShare.value = "Share";
	btnShare.addEventListener("click", saveCombat, false);
	div.appendChild(btnShare);
	div.appendChild(document.createElement("br"));
	div.appendChild(document.createElement("br"));
	var br = document.getElementsByTagName("br")[1];
	br.parentNode.insertBefore(div, br.nextSibling);
}

addShareBtn();